$(function () {
    $('body').on('click','#resetPassword',function () {
        var userId = $(this).data('id');
        resetUserPassword(userId);
    }).on('click','.unlockAndHistory',function () {//单个搜索
        var userIds = $(this).data('id');
        var isavalible = $(this).data('isavalible');
        var obj = $(this);
        if(isavalible==1){
            updateUserStatusByUserIds(userIds, obj, 2);
        }
        if(isavalible==2){
            updateUserStatusByUserIds(userIds, obj, 1);
        }
    }).on('click', '.selectCourse', function () {
        selectCourse({
            selectType:"checkbox",
            type:1,
            callback:'selectCourseCallback'
        });
    });
    bonusPoints();
    personsNumIncome();
});

/**
 *
 */
function personsNumIncome() {
    var userId = $("#userIdForm input[name='userId']").val();
    $.ajax({
        url: baseloaction + "/admin/user/cus/personsNumIncome",
        data: {"userId": userId},
        type: "post",
        dataType: "json",
        success: function (result) {
            if (result.success) {
                var _entity=result.entity;
                if(result.entity!=null){
                    $("#customerSunNum").html(_entity.customerNum+_entity.sunNum);
                    if(_entity.agentIncome!=null){
                        if(_entity.agentIncome.incomeSumPrice!=0 &&_entity.agentIncome.incomeSumPrice!=null){
                            $("#agentIncome").html(_entity.agentIncome.incomeSumPrice);
                        }
                    }
                }
            }
        }
    });
}

/**
 * 批量封禁，批量恢复
 * @param userIds
 * @param obj
 * @param isavalible
 */
function updateUserStatusByUserIds(userIds, obj, isavalible) {
    $.ajax({
        url: baseloaction + "/admin/user/cus/updateCusUserStatus",
        data: {"userIds": userIds, "status": isavalible},
        type: "post",
        dataType: "json",
        success: function (result) {
            if (result.success) {
                if (2 == isavalible) {
                    obj.html("");
                    obj.data('isavalible', '2');
                    obj.append('<i class="fa fa-history"></i> 恢复登录')
                }
                if (1 == isavalible) {
                    obj.html("");
                    obj.data('isavalible', '1');
                    obj.append('<i class="fa fa-unlock-alt"></i></i> 封禁登录')
                }
            } else {
                icModale(0, result.message);
            }
        },
        error: function (error) {
            icModale(0, "系统繁忙,请稍后重试");
        }
    })
}


/**
 * 重置密码
 * @param userId
 */
function resetUserPassword(userId) {
    var index = parent.layer.open({
        type: 1,
        area: ['380px'],
        skin: 'layui-layer-demo',
        title: '重置密码',
        content: '<div class="m-t m-r m-l m-b"><input class="form-control"  value="" id="rPassword" name="" type="text"></div>',
        btn: '确定',
        yes: function (index, layero) {
            var rPassword = layero.find('#rPassword').val();
            if(rPassword==null){
                return icModale(0, "请输入重置密码");
            }
            $.ajax({
                url:  baseloaction + "/admin/user/cus/resetPassword",
                type: "post",
                data: {"userId": userId, "rPassword": rPassword},
                dataType: "json",
                success: function (result) {
                    if (result.success == true) {
                        icModale(0, result.message);
                        parent.layer.close(index);
                    } else {
                        icModale(0, result.message);
                    }
                },
                error: function (error) {
                    parent.layer.close(index);
                    icModale(0, '系统繁忙，请稍后再试');
                }
            });
        }
    });
}

//表格中单个字段的修改添加奖励积分
function bonusPoints() {
    $('body').on('click', '#resetdBtn', function () {
        var id = $(this).data('id');
        var index = parent.layer.open({
            type: 1,
            area: ['380px'],
            skin: 'layui-layer-demo',
            title: '奖励积分',
            content: '<div class="m-t m-r m-l m-b"><input class="form-control"  value="" id="userScore" name="" type="text"></div>',
            btn: '确定',
            yes: function (index, layero) {
                var userScore = layero.find('#userScore').val();
                if(!isNumber(userScore)){
                    return icModale(0, "请正确输入奖励积分");
                }
                if(userScore==0){
                    return icModale(0, "赠送积分不能为0");
                }
                $.ajax({
                    url: baseloaction + "/admin/user/cus/addUserIntegral",
                    type: "post",
                    data: {"userId": id, "score": userScore},
                    dataType: "json",
                    success: function (result) {
                        parent.layer.close(index);
                        if (result.success == true) {
                            icModale(0, result.message);
                        } else {
                            icModale(0, result.message);
                        }
                    },
                    error: function (error) {
                        parent.layer.close(index);
                        icModale(0, '系统繁忙，请稍后再试');
                    }
                });
            }
        });
    });
}
